package com.admin.action.login_verify_action;

import com.admin.Action;
import com.admin.service.login_verify_service.LoginVerifyServiceImpl;
import com.pojo.AdminBean;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

import static com.utils.EncryptUtils.desEncrypt;

public class AdminRegisterAction extends Action {
    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        PrintWriter out=response.getWriter();
        HttpSession session = request.getSession();
        if(session.getAttribute("adminInfo")==null){
            out.println("<script>parent.window.location.href='error.html';</script>");
            return;
        }

        LoginVerifyServiceImpl loginVerifyService = new LoginVerifyServiceImpl();
        String username = request.getParameter("username");
        String phone = request.getParameter("phone");
        String pwd=request.getParameter("pwd");
        String adminLevel = request.getParameter("admin");

        AdminBean ab=new AdminBean();
        ab.setAdminName(username);

        try {
            //编码
            pwd=desEncrypt(pwd);
        } catch (Exception e) {
            e.printStackTrace();
        }

        ab.setAdminPwd(pwd);
        ab.setAdminLevel(Integer.valueOf(adminLevel));
        ab.setAdminPhone(phone);

        boolean bool=loginVerifyService.adminRegister(ab);
        System.out.println("注册:"+bool);
        if (bool){
            out.print(true);
        }else {
            out.print(false);
        }
    }
}
